package cn.lanqiao.book.controller;

import cn.lanqiao.book.dao.IBookDao;
import cn.lanqiao.book.dao.IChapterDao;
import cn.lanqiao.book.dao.impl.BookDaoImplI;
import cn.lanqiao.book.dao.impl.ChapterDaoImplI;
import cn.lanqiao.book.pojo.Book;
import cn.lanqiao.book.pojo.chapter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class chapterServlet extends HttpServlet {

    IChapterDao chd = new ChapterDaoImplI();
    chapter ch = new chapter();
    Book bo = new Book();
    IBookDao bd = new BookDaoImplI();


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("_method");
        try {
            Method m = getClass().getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            m.invoke(this, req, resp);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public void addChapter(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Object type = session.getAttribute("type");
        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            if (type1 == 3 || type1 == 2) {
                int bid = Integer.parseInt(req.getParameter("b_id"));
                String name = req.getParameter("ch_name");
                String content = req.getParameter("ch_content");

                ch.setB_id(bid);
                ch.setCh_name(name);
                ch.setCh_content(content);
                chd.addChapter(ch);
                req.getRequestDispatcher("/chapterman.jsp").forward(req, resp);
            }

        }

    }

    public void deleteChapter(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();

        Object type = session.getAttribute("type");
        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            if (type1 == 3 || type1 == 2) {
                int bid = Integer.parseInt(req.getParameter("b_id"));
               bo =  bd.queryForOnebybid(bid);
                int id = bo.getC_id();
                int chid = Integer.parseInt(req.getParameter("ch_id"));
                    chd.deleteChapterbycid(chid);
                req.getRequestDispatcher("/chapterman.jsp").forward(req, resp);
            } else {
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
            }

        }
    }

    public void selectChapter(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        Object type = session.getAttribute("type");

        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            if (type1 == 2 || type1 == 3) {
                List<chapter> l = new ArrayList<>();
                l = chd.queryAll();
                req.setAttribute("chapter", l);
                req.getRequestDispatcher("/selectChapter.jsp").forward(req, resp);
            } else {
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
            }
        }
    }

    public void qurryChapter(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        HttpSession session = req.getSession();
        Object type = session.getAttribute("type");
        if (type == null) {
            req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
        } else {
            int type1 = (int) type;
            if (type1 == 3 || type1 == 2) {
                String name = req.getParameter("ch_name");
                ch = chd.queryForOne(name);
                req.setAttribute("chapter", ch);
                req.getRequestDispatcher("/update3.jsp").forward(req, resp);
            } else {
                req.getRequestDispatcher("/invalidateLogin.jsp").forward(req, resp);
            }
        }
    }

    public void updateChapter(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int bid = Integer.parseInt(req.getParameter("b_id"));
        String oldname = req.getParameter("oldchapter");
        String content = req.getParameter("ch_content");
        String newname = req.getParameter("newchapter");

        int chid = chd.queryForOnebyname(bid, oldname).getCh_id();
        ch.setB_id(bid);
        ch.setCh_name(newname);
        ch.setCh_content(content);
        ch.setCh_id(chid);
        chd.updateChapter(ch);
        req.getRequestDispatcher("/chapterman.jsp").forward(req, resp);

    }

    public void searchChapter(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html; charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String name = req.getParameter("ch_name");
        name = "%" + name + "%";
        List<chapter> l = new ArrayList<>();
        l = chd.search(name);
        req.setAttribute("chapter3", l);
        req.getRequestDispatcher("/search4.jsp").forward(req, resp);
    }

}
